package com.bc.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;

import com.bc.db.entity.BtcNews;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DBUtils {
	public static final String DATABASE_NAME = "btc_news.sqlite3";
	public static final String TABLE_NAME = "btc_news";

	public static void copyDbNewsIfNeeded(Context context) {
		if (new File(context.getFilesDir(), DATABASE_NAME).exists()) {
			return;
		}
		Log.i("DBUtils", "开始复制数据库");
		try {
			FileOutputStream fos = context.openFileOutput(DATABASE_NAME, Context.MODE_PRIVATE);

			InputStream is = context.getResources().getAssets().open(DATABASE_NAME);
			byte[] buffer = new byte[1024 * 4];
			int len = 0;
			while ((len = is.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			fos.close();
			is.close();
			Log.i("DBUtils", "数据库复制成功");

		} catch (Exception e) {
			e.printStackTrace();
			Log.e("DBUtils", "复制数据库时发生意外：" + e.getMessage());
		}
	}

	
	public static ArrayList<BtcNews> getEnabledBtcNews(Context context) {
		File file = new File(context.getFilesDir(), DATABASE_NAME);
		SQLiteDatabase db = null;
		Cursor cursor = null;
		ArrayList<BtcNews> btcNewsList = new ArrayList<BtcNews>();
		try {
			db = context.openOrCreateDatabase(file.getAbsolutePath(), Context.MODE_PRIVATE, null);
			cursor = db.query(TABLE_NAME, 
					new String[]{Constants.BTC_NAME_COL, Constants.BTC_ICON_COL, Constants.BTC_TYPE_COL, Constants.BTC_ENABLE_COL}, 
					Constants.BTC_ENABLE_COL+"=?", 
					new String[]{"1"}, 
					null, 
					null, 
					null);
			
			BtcNews btcNews = null;
			while (cursor.moveToNext()) {
				btcNews = new BtcNews();
				btcNews.btcName = cursor.getString(0);
				btcNews.btcIcon = cursor.getString(1);
				btcNews.btcType = cursor.getString(2);
				btcNews.enable = cursor.getInt(3);
				btcNewsList.add(btcNews);
			}
		} catch (Exception e) {
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}
		return btcNewsList;
	}
	
	
	public static ArrayList<BtcNews> getAllBtcNews(Context context) {
		File file = new File(context.getFilesDir(), DATABASE_NAME);
		SQLiteDatabase db = null;
		Cursor cursor = null;
		ArrayList<BtcNews> btcNewsList = new ArrayList<BtcNews>();
		try {
			db = context.openOrCreateDatabase(file.getAbsolutePath(), Context.MODE_PRIVATE, null);
			cursor = db.query(TABLE_NAME, 
					new String[]{Constants.BTC_ID, Constants.BTC_NAME_COL, Constants.BTC_ICON_COL, Constants.BTC_TYPE_COL, Constants.BTC_ENABLE_COL}, 
					null, null, null, null, null);
			BtcNews btcNews = null;
			while (cursor.moveToNext()) {
				btcNews = new BtcNews();
				btcNews.id = cursor.getInt(0);
				btcNews.btcName = cursor.getString(1);
				btcNews.btcIcon = cursor.getString(2);
				btcNews.btcType = cursor.getString(3);
				btcNews.enable = cursor.getInt(4);
				btcNewsList.add(btcNews);
			}
		} catch (Exception e) {
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}
		return btcNewsList;
	}
	
	
	public static void updateBtcEnables(Context context, ArrayList<BtcNews> newsList) {
		File file = new File(context.getFilesDir(), DATABASE_NAME);
		SQLiteDatabase db = null;
		try {
			db = context.openOrCreateDatabase(file.getAbsolutePath(), Context.MODE_PRIVATE, null);
			db.beginTransaction();
			ContentValues values;
			for (BtcNews btcNews : newsList) {
				values = new ContentValues();
				values.put(Constants.BTC_ENABLE_COL, btcNews.enable);
				db.update(TABLE_NAME, values, Constants.BTC_ID + "=?",new String[]{btcNews.id+""});
			}
			db.setTransactionSuccessful();
		} catch (Exception e) {
		} finally {
			db.endTransaction();
			if (db != null) {
				db.close();
			}
		}
	}
}
